package com.ruoyi.api.service.impl;

import com.ruoyi.api.domain.GuidDayEntity;
import com.ruoyi.api.domain.GuidDayScenicEntity;
import com.ruoyi.api.domain.GuidEntity;
import com.ruoyi.api.domain.ScenicEntity;
import com.ruoyi.api.repository.GuidDayScenicRepository;
import com.ruoyi.api.service.GuidDayScenicService;
import com.ruoyi.api.service.GuidDayService;
import com.ruoyi.api.service.UserGuidService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class GuidDayScenicServiceImpl implements GuidDayScenicService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private GuidDayScenicRepository guidDayScenicRepository;

    @Override
    public void deleteByGuidId(String id) {
        guidDayScenicRepository.deleteByGuidId(id);

    }

    @Override
    public void save(List<GuidDayScenicEntity> list) {
        guidDayScenicRepository.saveAll(list);
    }

    @Override
    public List<String> selectGuidIdByScenicId(List<String> where) {
        // 创建聚合管道
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(Criteria.where("scenicId").in(where)),
                Aggregation.project("guidId").andExclude("_id")  // 只映射 guidId 字段并排除 _id 字段
        );

        // 执行聚合查询
        AggregationResults<GuidIdResult> results = mongoTemplate.aggregate(aggregation, "guid_day_scenic", GuidIdResult.class);

        // 从结果中提取 guidId
        List<GuidIdResult> guidIds = results.getMappedResults();

        // 返回唯一去重的 guidId 列表
        return guidIds.stream().map(GuidIdResult::getGuidId).distinct().collect(Collectors.toList());
    }

    @Override
    public List<GuidDayScenicEntity> getScenicIdsByGuidDayId(String id) {
        GuidDayScenicEntity guidDayScenicEntity = new GuidDayScenicEntity();
        guidDayScenicEntity.setGuidDayId(id);
        List<GuidDayScenicEntity> list =  this.selectAll(GuidDayScenicEntity.class, guidDayScenicEntity, mongoTemplate);
        List<String> list1 = new ArrayList<>();

        return list;
    }

    @Override
    public List<ScenicEntity> selectAllByIds(List<ObjectId> scenicIds) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").in(scenicIds));
        return mongoTemplate.find(query,ScenicEntity.class);
    }

    // 定义一个结果类来映射聚合结果
    public class GuidIdResult {
        private String guidId;

        public String getGuidId() {
            return guidId;
        }

        public void setGuidId(String guidId) {
            this.guidId = guidId;
        }
    }
}
